/**
 * 渠道价格维护 - 编辑页面
 * @author shixc
 * @date 2019/02/01
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            gwdb003: false,
            detail: false,
            form: {
                keyword1: '',
                keyword2: ''
            },
            rules: {}
        },
        cols1 = [[
            {field: 'GWAK001', type: 'checkbox', title: '选择'}
            , {field: 'GWAK010', width: 80, title: '客户渠道说明'}
            , {field: 'GWAK001', width: 80, title: '客户编号'}
            , {field: 'GWAKL004', width: 120, title: '客户名称'}
        ]],
        cols2 = [[
            {field: 'GWAM001', type: 'checkbox', title: '选择'}
            , {field: 'GWAOL003', width: 80, title: '产品系列说明'}
            , {field: 'GWAM001', width: 120, title: '品号'}
            , {field: 'GWAML003', width: 120, title: '品名'}
        ]],
        table_gwak = {},
        table_gwam = {},
        listData = null,
        pageVue,
        table, layOpenIndex, gwakTable, gwamTable
    ;

    var Gwdc001Auto = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwdc001_auto',
                    _loadTplsFull: [
                        {dom: 'common_open_window', tpl: 'common/_open_window'}
                    ]
                }, win);
            },
            initTable: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwakTable = table.render({
                        elem: app.getByid(doc, 'gwdc001_gwak_table'),
                        id: 'gwdc001_gwak_table',
                        url: g_ctxPath + 'crm/gwdc001/getGwakData',
                        // size: _layuiConfig.ui_size,
                        // autoSort: false, //禁用前端自动排序
                        cols: cols1,
                        data: listData,
                        //loading: true,
                        page: true,
                        height: 500,
                        where: table_gwak
                    });
                    gwamTable = table.render({
                        elem: app.getByid(doc, 'gwdc001_gwam_table'),
                        id: 'gwdc001_gwam_table',
                        url: g_ctxPath + 'crm/gwdc001/getGwamData',
                        // size: _layuiConfig.ui_size,
                        // autoSort: false, //禁用前端自动排序
                        cols: cols2,
                        data: listData,
                        //loading: true,
                        page: true,
                        height: 500,
                        where: table_gwam
                    });
                });
            },

            tableReload1: function () {
                self._reloadTable({id: 'gwdc001_gwak_table', where: table_gwak});
            },

            tableReload2: function () {
                self._reloadTable({id: 'gwdc001_gwam_table', where: table_gwam});
            },

            _call: function (tagData) {
                pageVue.$refs['form'].resetFields();
                vueData.form.keyword1 = '';
                vueData.form.keyword2 = '';
                layOpenIndex = tagData.layOpenIndex;
                self.tableReload1();
                self.tableReload2();
            },
            _ready: function () {
                table = layui.table;
                pageVue = new Vue({
                    el: '#gwdc001_auto',
                    data: vueData,
                    watch: {},
                    methods: {
                        cancel: function () {
                            layer.close(layOpenIndex);
                        },
                        sure: function () {
                            var gwakData = table.checkStatus('gwdc001_gwak_table');
                            var gwamData = table.checkStatus('gwdc001_gwam_table');
                            if (gwakData.data.length == 0 || gwamData.data.length == 0) {
                                app.showWarning('请先选择客户和商品信息！');
                                return;
                            }
                            var _data = [];
                            gwakData.data.forEach(function (v) {
                                var _temp = app.fields_('GWDA');
                                gwamData.data.forEach(function (item) {
                                    _temp.GWDA002 = v.GWAK001;
                                    _temp.GWDA003 = item.GWAM001;
                                    _temp.GWDA004 = new Date().Format('yyyy-MM-dd');
                                    _temp.GWDA005 = 'Y';
                                    _temp.GWDA006 = 'Y';
                                    _temp.GWDA007 = item.GWAM004;
                                    _data.push(_temp);
                                })
                            });
                            app.ajax({
                                xurl: 'crm/gwdc001/addData',
                                xdata: _data,
                                loading: true,
                                toJsonStr: true,
                                okmsg: '商品经营范围增加成功',
                                cb: function (result) {
                                    if (result.ok) {
                                        app.pageModule('gwdc001').reloadTableCurr();
                                        layer.close(layOpenIndex);
                                    }
                                }
                            });
                        },
                        openQd: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    multiple: true,
                                    config: {
                                        method: 'q_gwae001',
                                        columnList: [
                                            {label: '销售渠道编号', prop: 'GWAE001', width: '150'},
                                            {label: '说明', prop: 'GWAEL003', width: '150'},
                                            {label: '助记码', prop: 'GWAEL004', width: '150'}
                                        ],
                                        dataList: [],
                                        key: 'GWAE001'
                                    },
                                    cb: function (data) {
                                        if(data.length > 0){
                                            var gwae001 = '';
                                            for(var i = 0; i < data.length; i ++){
                                                if(i == 0){
                                                    vueData.form.keyword1 = data[i].GWAEL003;
                                                    gwae001 = data[i].GWAE001;
                                                }else{
                                                    vueData.form.keyword1 = vueData.form.keyword1 + ',' + data[i].GWAEL003;
                                                    gwae001 = gwae001 + ',' + data[i].GWAE001;
                                                }
                                            }
                                        }
                                        table_gwak.keyword = gwae001;
                                        self.tableReload1();
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择客户渠道',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        openXl: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    multiple: true,
                                    config: {
                                        method: 'q_gwao001',
                                        columnList: [{label: '系列编号', prop: 'GWAO001', width: '150'},
                                            {label: '说明', prop: 'GWAOL003', width: '150'},
                                            {label: '助记码', prop: 'GWAOL004', width: '150'}
                                        ],
                                        dataList: [],
                                        key: 'GWAO001'
                                    },
                                    cb: function (data) {
                                        if(data.length > 0){
                                            var gwao001 = '';
                                            for(var i = 0; i < data.length; i ++){
                                                if(i == 0){
                                                    vueData.form.keyword2 = data[i].GWAOL003;
                                                    gwao001 = data[i].GWAO001;
                                                }else{
                                                    vueData.form.keyword2 = vueData.form.keyword2 + ',' + data[i].GWAOL003;
                                                    gwao001 = gwao001 + ',' + data[i].GWAO001;
                                                }
                                            }
                                        }
                                        table_gwam.keyword = gwao001;
                                        self.tableReload2();
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择产品系列',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        reset1: function () {
                           vueData.form.keyword1 = '';
                           table_gwak = {};
                           self.tableReload1();
                        },
                        reset2: function () {
                            vueData.form.keyword2 = '';
                            table_gwam = {};
                            self.tableReload2();
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwdc001_auto');
                    }
                });
                self.initTable();
            },
            _lazy: function () {
                this._super();
            }
        })
    ;

    return new Gwdc001Auto();

})
;
